
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>动态组件 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="directive.html" />
    
    
    <link rel="prev" href="style.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../Angular-cli/">
            
                <a href="../Angular-cli/">
            
                    
                    Angular-cli
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../Angular-cli/Angular-cli.html">
            
                <a href="../Angular-cli/Angular-cli.html">
            
                    
                    安装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../Angular-cli/restMenu.html">
            
                <a href="../Angular-cli/restMenu.html">
            
                    
                    初始结构
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../architecture/">
            
                <a href="../architecture/">
            
                    
                    架构
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../architecture/mo-kuai.html">
            
                <a href="../architecture/mo-kuai.html">
            
                    
                    模块简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../architecture/component.html">
            
                <a href="../architecture/component.html">
            
                    
                    组件简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../architecture/Service.html">
            
                <a href="../architecture/Service.html">
            
                    
                    服务与依赖注入
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="./">
            
                <a href="./">
            
                    
                    组件与模板
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="zu-jian-yu-mo-ban.html">
            
                <a href="zu-jian-yu-mo-ban.html">
            
                    
                    数据显示与模板语法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="cycle.html">
            
                <a href="cycle.html">
            
                    
                    生命周期钩子
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="component.html">
            
                <a href="component.html">
            
                    
                    组件交互
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="style.html">
            
                <a href="style.html">
            
                    
                    组件样式
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.4.5" data-path="dynamic.html">
            
                <a href="dynamic.html">
            
                    
                    动态组件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="directive.html">
            
                <a href="directive.html">
            
                    
                    属性指令与结构指令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="pipeAndAnimit.html">
            
                <a href="pipeAndAnimit.html">
            
                    
                    管道与动画
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../router/">
            
                <a href="../router/">
            
                    
                    路由与导航
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../form/">
            
                <a href="../form/">
            
                    
                    表单
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../http/">
            
                <a href="../http/">
            
                    
                    请求http
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../project/">
            
                <a href="../project/">
            
                    
                    项目解析:结构目录
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >动态组件</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h3 id="&#x52A8;&#x6001;&#x7EC4;&#x4EF6;&#x52A0;&#x8F7D;">&#x52A8;&#x6001;&#x7EC4;&#x4EF6;&#x52A0;&#x8F7D;</h3>
<p>&#x6307;&#x4EE4;</p>
<p>   &#x5728;&#x6DFB;&#x52A0;&#x7EC4;&#x4EF6;&#x4E4B;&#x524D;&#xFF0C;&#x5148;&#x8981;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x951A;&#x70B9;&#x6765;&#x544A;&#x8BC9; Angular &#x8981;&#x628A;&#x7EC4;&#x4EF6;&#x63D2;&#x5165;&#x5230;&#x4EC0;&#x4E48;&#x5730;&#x65B9;&#x3002;</p>
<p>   &#x5E7F;&#x544A;&#x6761;&#x4F7F;&#x7528;&#x4E00;&#x4E2A;&#x540D;&#x53EB; <code>AdDirective</code> &#x7684;&#x8F85;&#x52A9;&#x6307;&#x4EE4;&#x6765;&#x5728;&#x6A21;&#x677F;&#x4E2D;&#x6807;&#x8BB0;&#x51FA;&#x6709;&#x6548;&#x7684;&#x63D2;&#x5165;&#x70B9;&#x3002;</p>
<pre><code>    import { Directive, ViewContainerRef } from &apos;@angular/core&apos;;

    @Directive({
      selector: &apos;[ad-host]&apos;,
    })
    export class AdDirective {
      constructor(public viewContainerRef: ViewContainerRef) { }
    }
</code></pre><p>AdDirective &#x6CE8;&#x5165;&#x4E86; <code>ViewContainerRef</code> &#x6765;&#x83B7;&#x53D6;&#x5BF9;&#x5BB9;&#x5668;&#x89C6;&#x56FE;&#x7684;&#x8BBF;&#x95EE;&#x6743;&#xFF0C;&#x8FD9;&#x4E2A;&#x5BB9;&#x5668;&#x5C31;&#x662F;&#x90A3;&#x4E9B;&#x52A8;&#x6001;&#x52A0;&#x5165;&#x7684;&#x7EC4;&#x4EF6;&#x7684;&#x5BBF;&#x4E3B;&#x3002;</p>
<p>&#x52A0;&#x8F7D;&#x7EC4;&#x4EF6;</p>
<p><code>&lt;ng-template&gt;</code>&#x5143;&#x7D20;&#x5C31;&#x662F;&#x521A;&#x624D;&#x5236;&#x4F5C;&#x7684;&#x6307;&#x4EE4;&#x5C06;&#x5E94;&#x7528;&#x5230;&#x7684;&#x5730;&#x65B9;&#x3002; 
&#x8981;&#x5E94;&#x7528; <code>AdDirective</code>&#xFF0C;&#x56DE;&#x5FC6;&#x4E00;&#x4E0B;&#x6765;&#x81EA; ad.directive.ts &#x7684;&#x9009;&#x62E9;&#x5668; ad-host&#x3002;
&#x628A;&#x5B83;&#x5E94;&#x7528;&#x5230;<code>&lt;ng-template&gt;</code>&#xFF08;&#x4E0D;&#x7528;&#x5E26;&#x65B9;&#x62EC;&#x53F7;&#xFF09;&#x3002; &#x8FD9;&#x4E0B;&#xFF0C;Angular &#x5C31;&#x77E5;&#x9053;&#x8BE5;&#x628A;&#x7EC4;&#x4EF6;&#x52A8;&#x6001;&#x52A0;&#x8F7D;&#x5230;&#x54EA;&#x91CC;&#x4E86;&#x3002;</p>
<pre><code>    template: `
                &lt;div class=&quot;ad-banner&quot;&gt;
                  &lt;h3&gt;Advertisements&lt;/h3&gt;
                  &lt;ng-template ad-host&gt;&lt;/ng-template&gt;
                &lt;/div&gt;
          `
</code></pre><p><code>&lt;ng-template&gt;</code> &#x5143;&#x7D20;&#x662F;&#x52A8;&#x6001;&#x52A0;&#x8F7D;&#x7EC4;&#x4EF6;&#x7684;&#x6700;&#x4F73;&#x9009;&#x62E9;&#xFF0C;&#x56E0;&#x4E3A;&#x5B83;&#x4E0D;&#x4F1A;&#x6E32;&#x67D3;&#x4EFB;&#x4F55;&#x989D;&#x5916;&#x7684;&#x8F93;&#x51FA;&#x3002;</p>
<p>&#x89E3;&#x6790;&#x7EC4;&#x4EF6;</p>
<p>&#x6DF1;&#x5165;&#x770B;&#x770B; <code>ad-banner.component.ts</code> &#x4E2D;&#x7684;&#x65B9;&#x6CD5;&#x3002;</p>
<p><code>AdBannerComponent</code> &#x63A5;&#x6536;&#x4E00;&#x4E2A; <code>AdItem</code> &#x5BF9;&#x8C61;&#x7684;&#x6570;&#x7EC4;&#x4F5C;&#x4E3A;&#x8F93;&#x5165;&#xFF0C;&#x5B83;&#x6700;&#x7EC8;&#x6765;&#x81EA; <code>AdService</code>&#x3002; <code>AdItem</code> &#x5BF9;&#x8C61;&#x6307;&#x5B9A;&#x8981;&#x52A0;&#x8F7D;&#x7684;&#x7EC4;&#x4EF6;&#x7C7B;&#xFF0C;&#x4EE5;&#x53CA;&#x7ED1;&#x5B9A;&#x5230;&#x8BE5;&#x7EC4;&#x4EF6;&#x4E0A;&#x7684;&#x4EFB;&#x610F;&#x6570;&#x636E;&#x3002; AdService &#x53EF;&#x4EE5;&#x8FD4;&#x56DE;&#x5E7F;&#x544A;&#x6D3B;&#x52A8;&#x4E2D;&#x7684;&#x90A3;&#x4E9B;&#x5E7F;&#x544A;&#x3002;</p>
<p>&#x7ED9; <code>AdBannerComponent</code> &#x4F20;&#x5165;&#x4E00;&#x4E2A;&#x7EC4;&#x4EF6;&#x6570;&#x7EC4;&#x53EF;&#x4EE5;&#x5728;&#x6A21;&#x677F;&#x4E2D;&#x653E;&#x5165;&#x4E00;&#x4E2A;&#x5E7F;&#x544A;&#x7684;&#x52A8;&#x6001;&#x5217;&#x8868;&#xFF0C;&#x800C;&#x4E0D;&#x7528;&#x5199;&#x6B7B;&#x5728;&#x6A21;&#x677F;&#x4E2D;&#x3002;</p>
<p>&#x901A;&#x8FC7; <code>getAds()</code> &#x65B9;&#x6CD5;&#xFF0C;<code>AdBannerComponent</code> &#x53EF;&#x4EE5;&#x5FAA;&#x73AF;&#x904D;&#x5386; <code>AdItems</code> &#x7684;&#x6570;&#x7EC4;&#xFF0C;&#x5E76;&#x4E14;&#x6BCF;&#x4E09;&#x79D2;&#x8C03;&#x7528;&#x4E00;&#x6B21; <code>loadComponent()</code> &#x6765;&#x52A0;&#x8F7D;&#x65B0;&#x7EC4;&#x4EF6;&#x3002;</p>
<pre><code>    export class AdBannerComponent implements OnInit, OnDestroy {
      @Input() ads: AdItem[];
      currentAdIndex = -1;
      @ViewChild(AdDirective) adHost: AdDirective;
      interval: any;

      constructor(private componentFactoryResolver: ComponentFactoryResolver) { }

      ngOnInit() {
        this.loadComponent();
        this.getAds();
      }

      ngOnDestroy() {
        clearInterval(this.interval);
      }

      loadComponent() {
        this.currentAdIndex = (this.currentAdIndex + 1) % this.ads.length;
        let adItem = this.ads[this.currentAdIndex];

        let componentFactory = this.componentFactoryResolver.resolveComponentFactory(adItem.component);

        let viewContainerRef = this.adHost.viewContainerRef;
        viewContainerRef.clear();

        let componentRef = viewContainerRef.createComponent(componentFactory);
        (&lt;AdComponent&gt;componentRef.instance).data = adItem.data;
      }

      getAds() {
        this.interval = setInterval(() =&gt; {
          this.loadComponent();
        }, 3000);
      }
    }
</code></pre><p>&#x5728; <code>loadComponent()</code> &#x9009;&#x53D6;&#x4E86;&#x4E00;&#x4E2A;&#x5E7F;&#x544A;&#x4E4B;&#x540E;&#xFF0C;&#x5B83;&#x4F7F;&#x7528; <code>ComponentFactoryResolver</code> &#x6765;&#x4E3A;&#x6BCF;&#x4E2A;&#x5177;&#x4F53;&#x7684;&#x7EC4;&#x4EF6;&#x89E3;&#x6790;&#x51FA;&#x4E00;&#x4E2A; <code>ComponentFactory</code>&#x3002; &#x7136;&#x540E; <code>ComponentFactory</code> &#x4F1A;&#x4E3A;&#x6BCF;&#x4E00;&#x4E2A;&#x7EC4;&#x4EF6;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x5B9E;&#x4F8B;&#x3002;</p>
<p>&#x63A5;&#x4E0B;&#x6765;&#xFF0C;&#x4F60;&#x8981;&#x628A; <code>viewContainerRef</code> &#x6307;&#x5411;&#x8FD9;&#x4E2A;&#x7EC4;&#x4EF6;&#x7684;&#x73B0;&#x6709;&#x5B9E;&#x4F8B;&#x3002;&#x4F46;&#x4F60;&#x600E;&#x4E48;&#x624D;&#x80FD;&#x627E;&#x5230;&#x8FD9;&#x4E2A;&#x5B9E;&#x4F8B;&#x5462;&#xFF1F; &#x5F88;&#x7B80;&#x5355;&#xFF0C;&#x56E0;&#x4E3A;&#x5B83;&#x6307;&#x5411;&#x4E86; <code>adHost</code>&#xFF0C;&#x800C;&#x8FD9;&#x4E2A; adHost &#x5C31;&#x662F;&#x4F60;&#x4EE5;&#x524D;&#x8BBE;&#x7F6E;&#x8FC7;&#x7684;&#x6307;&#x4EE4;&#xFF0C;&#x7528;&#x6765;&#x544A;&#x8BC9; Angular &#x8BE5;&#x628A;&#x52A8;&#x6001;&#x7EC4;&#x4EF6;&#x63D2;&#x5165;&#x5230;&#x4EC0;&#x4E48;&#x4F4D;&#x7F6E;&#x3002;</p>
<p>&#x56DE;&#x5FC6;&#x4E00;&#x4E0B;&#xFF0C;AdDirective &#x66FE;&#x5728;&#x5B83;&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#x4E2D;&#x6CE8;&#x5165;&#x4E86;&#x4E00;&#x4E2A;<code>ViewContainerRef</code>&#x3002; &#x56E0;&#x6B64;&#x8FD9;&#x4E2A;&#x6307;&#x4EE4;&#x53EF;&#x4EE5;&#x8BBF;&#x95EE;&#x5230;&#x8FD9;&#x4E2A;&#x4F60;&#x6253;&#x7B97;&#x7528;&#x4F5C;&#x52A8;&#x6001;&#x7EC4;&#x4EF6;&#x5BBF;&#x4E3B;&#x7684;&#x5143;&#x7D20;&#x3002;</p>
<p>&#x8981;&#x628A;&#x8FD9;&#x4E2A;&#x7EC4;&#x4EF6;&#x6DFB;&#x52A0;&#x5230;&#x6A21;&#x677F;&#x4E2D;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x8C03;&#x7528; <code>ViewContainerRef</code> &#x7684; <code>createComponent()</code>&#x3002;</p>
<p><code>createComponent()</code>&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x5F15;&#x7528;&#xFF0C;&#x6307;&#x5411;&#x8FD9;&#x4E2A;&#x521A;&#x521A;&#x52A0;&#x8F7D;&#x7684;&#x7EC4;&#x4EF6;&#x3002; &#x4F7F;&#x7528;&#x8FD9;&#x4E2A;&#x5F15;&#x7528;&#x5C31;&#x53EF;&#x4EE5;&#x4E0E;&#x8BE5;&#x7EC4;&#x4EF6;&#x8FDB;&#x884C;&#x4EA4;&#x4E92;&#xFF0C;&#x6BD4;&#x5982;&#x8BBE;&#x7F6E;&#x5B83;&#x7684;&#x5C5E;&#x6027;&#x6216;&#x8C03;&#x7528;&#x5B83;&#x7684;&#x65B9;&#x6CD5;&#x3002;</p>
<p>&#x5BF9;&#x9009;&#x62E9;&#x5668;&#x7684;&#x5F15;&#x7528;</p>
<p>&#x8981;&#x60F3;&#x786E;&#x4FDD;&#x7F16;&#x8BD1;&#x5668;&#x7167;&#x5E38;&#x751F;&#x6210;&#x5DE5;&#x5382;&#x7C7B;&#xFF0C;&#x5C31;&#x8981;&#x628A;&#x8FD9;&#x4E9B;&#x52A8;&#x6001;&#x52A0;&#x8F7D;&#x7684;&#x7EC4;&#x4EF6;&#x6DFB;&#x52A0;&#x5230; NgModule &#x7684; <code>entryComponents</code> &#x6570;&#x7EC4;&#x4E2D;&#xFF1A;</p>
<pre><code>entryComponents: [ HeroJobAdComponent, HeroProfileComponent ],
</code></pre>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="style.html" class="navigation navigation-prev " aria-label="Previous page: 组件样式">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="directive.html" class="navigation navigation-next " aria-label="Next page: 属性指令与结构指令">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"动态组件","level":"1.4.5","depth":2,"next":{"title":"属性指令与结构指令","level":"1.4.6","depth":2,"path":"comAndtem/directive.md","ref":"comAndtem/directive.md","articles":[]},"previous":{"title":"组件样式","level":"1.4.4","depth":2,"path":"comAndtem/style.md","ref":"comAndtem/style.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["fontsettings","sharing","lunr","search","highlight","livereload"],"pluginsConfig":{"fontsettings":{"family":"sans","size":2,"theme":"white"},"sharing":{"all":["facebook","google","twitter","weibo","instapaper"],"facebook":true,"google":false,"instapaper":false,"twitter":true,"vk":false,"weibo":false},"lunr":{"ignoreSpecialCharacters":false,"maxIndexSize":1000000},"search":{},"highlight":{},"livereload":{},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"comAndtem/dynamic.md","mtime":"2018-06-14T07:32:05.238Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2018-06-29T06:02:19.129Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    

    </body>
</html>

